/*
 * File:        hal_frame.h
 * Author:      buquan_chen
 * Comments:    the new framework
 * Data:        2024/01/14
 * Revision history: V0.2
 */
#ifndef _HAL_FRAME_H
#define _HAL_FRAME_H

#include "ch32v20x.h"


//=================================================================================//
//                        Protocol configuration                   				   //
//Description:                                			   						   //
// 	1.Environment Definition(HAL_FRAME_ENVIRONMENT):                			   //
// 		1)0: Normal environment, 51, wch, etc;                                     //
// 		2)1: Use JL chip;                                    	   				   //
//=================================================================================//

#define HAL_FRAME_ENVIRONMENT 0


#define HAL_FRAME_DEBUG 1

//=================================================================================//
#if HAL_FRAME_ENVIRONMENT
#include "syscfg_id.h"  //Use the available IDs
#include "user_cfg_id.h"
#include "le_smartbox_module.h"
extern int app_send_user_data(u16 handle, u8 *data, u16 len, u8 handle_type);

#define CUSTOM_MODE_SIZE    512
#define CUSTOM_MODE_NUM     3

#define mode1   48
#define mode2   48
#define mode3   49
#define mode4   49
#define mode5   50
#define mode6   50

void hal_frame_mode_data_init();

#else

#define CUSTOM_MODE_SIZE    512
#define CUSTOM_MODE_NUM     3
#define custom_mode_based_adress    0

#define custom_mode1_adress         custom_mode_based_adress
#define custom_mode2_adress         custom_mode_based_adress + 512
#define custom_mode3_adress         custom_mode_based_adress + 512*2
#define custom_mode4_adress         custom_mode_based_adress + 512*3
#define custom_mode5_adress         custom_mode_based_adress + 512*4
#define custom_mode6_adress         custom_mode_based_adress + 512*5

#endif

#define LEVEL_MASK         0xC0           // Loop level mask
#define END_MASK           0X20           // End of frame mask
#define ACTION_FRAME_TYPE  1              // Action frame
#define CYCLE_FRAME_TYPE   2              // Cycle frame
#define END_FRAME_TYPE     3              // End frame
#define CYCLE01_FRAME_TYPE 0x40         // 1st level cycle
#define CYCLE02_FRAME_TYPE 0x80         // 2nd level cycle
#define CYCLE03_FRAME_TYPE 0xC0         // 3rd level cycle

#define CYCLE_OVER_TYPE 0x0F        //cycle address number exceeds 255

#define XXXX_TYPE  0x0F00     //some action setting

#define ACTION_BYTE 3

struct custom_step_type {
    uint8_t ctrol_set[ACTION_BYTE-1];
    uint8_t time;         // Time duration for the action
    uint8_t ctrol_len;
};
struct custom_frame{
    uint8_t  level;
    uint8_t  next_address;
    uint8_t  cycle_times;
    uint8_t  custom_frame_add[ACTION_BYTE-2];
};

union frame_type{
    uint8_t _data[ACTION_BYTE];
    struct custom_step_type action_frame;
    struct custom_frame     fun_frame;
};

struct custom_action_type{
    uint8_t level0_times;
    uint8_t level1_times;
    uint8_t level2_times;
    uint8_t level3_times;

    uint8_t action_id;
    uint16_t nxt_address;
    union frame_type frame;
};

void hal_frame_set_action(uint8_t id);
void hal_frame_action_init(void);
uint8_t hal_frame_get_ation(uint8_t *ctrol_object);
void hal_frame_analy_recv(uint8_t *pValue, uint16_t len);
void hal_frame_protocol_analy(uint8_t *buf,uint16_t len);

#endif
